package main.gui.settingsdialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

public class SettingsDialog extends JFrame implements ActionListener
{
	private static final long serialVersionUID = -4188462300202771458L;
	private static final String BROWSE = "Browse...";
	private static final String OPEN = "Open";
	private static final String CANCEL = "Cancel";
	private static final String OPEN_SETTINGS = "Load settings";
	private static final String MANUAL_SETTINGS = "settings";
	
	private JPanel m_settings;
	private JTabbedPane m_tabpane;
	private JTextField m_file;
	
	public SettingsDialog()
	{
		m_settings = new JPanel();
		m_settings.setLayout(new GridLayout(1, 1));
		m_tabpane = new JTabbedPane(JTabbedPane.TOP);
		
		openSettings();
		
		m_settings.add(m_tabpane);
		
		this.setSize(400, 300);
		this.setContentPane(m_settings);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		//this.pack();
	}
	
	public void openSettings()
	{
		JPanel panel = new JPanel();
		BoxLayout bl = new BoxLayout(panel, BoxLayout.Y_AXIS);
		panel.setLayout(bl);
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
			//boxlayout textfield + button
			JPanel jp = new JPanel();
			BoxLayout bl0 = new BoxLayout(jp, BoxLayout.X_AXIS);
			jp.setLayout(bl0);
		
				JLabel label = new JLabel("Choose a file");
				label.setFont(new Font("Default", Font.PLAIN, 18));
				label.setPreferredSize(new Dimension(400, 25));
				label.setMaximumSize(label.getPreferredSize());
				jp.add(label);
		
			//boxlayout textfield + button
			JPanel p = new JPanel();
			BoxLayout bl1 = new BoxLayout(p, BoxLayout.X_AXIS);
			p.setLayout(bl1);
			p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
			
				m_file = new JTextField();
				m_file.setPreferredSize(new Dimension(400, 25));
				m_file.setMaximumSize(m_file.getPreferredSize());
				p.add(m_file);
				
				p.add(Box.createHorizontalStrut(10));
	
				JButton open = new JButton(BROWSE);
				open.addActionListener(this);
				p.add(open);
			
			//buttonpanel
			JPanel buttonpanel = new JPanel();
			BoxLayout box = new BoxLayout(buttonpanel, BoxLayout.X_AXIS);
			buttonpanel.setLayout(box);
			buttonpanel.setPreferredSize(new Dimension(500, 40));
			buttonpanel.setMaximumSize(buttonpanel.getPreferredSize());
			
				buttonpanel.add(Box.createHorizontalGlue());
				
				JButton cancel = new JButton(CANCEL);
				cancel.addActionListener(this);
				buttonpanel.add(cancel);
				
				buttonpanel.add(Box.createHorizontalStrut(10));
				JButton save = new JButton(OPEN);
				buttonpanel.add(save);
		
		panel.add(jp, BorderLayout.PAGE_START);
		panel.add(p, BorderLayout.CENTER);
		panel.add(buttonpanel, BorderLayout.PAGE_END);
		
		m_tabpane.addTab(OPEN_SETTINGS, panel);
	}

	public void open()
	{
		SettingsPanel panel = new SettingsPanel();
		JFileChooser fs = new JFileChooser();
		//Show and accept .bps files
		fs.setFileFilter(new FileFilter() {
			@Override
			public boolean accept(File f) {
				if(f.isDirectory())
					return true;
				int start_extension = f.getName().lastIndexOf('.');
				if(f.getName().lastIndexOf('.') < 0)
					return false;
				else
				{
					String extension = f.getName().substring(start_extension);
					if(extension.equals(".bps"))
						return true;
				}
				return false;
			}

			@Override
			public String getDescription() {
			    return ".bps files";
			}
		});
		int code = fs.showOpenDialog(panel);
		if (code == JFileChooser.APPROVE_OPTION) {
            File file = fs.getSelectedFile();
            try {
            	m_file.setText(file.getAbsolutePath());
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getActionCommand().equals(BROWSE))
			open();
		else if(event.getActionCommand().equals(MANUAL_SETTINGS))
			;
		else if(event.getActionCommand().equals(CANCEL))
			this.setVisible(false);
		System.out.println(event.paramString());
	}
}
